// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
                // 这个属性引用了星星预制资源
                starPrefab: {
                    default: null,
                    type: cc.Prefab
                },
                // 星星产生后消失时间的随机范围
                maxStarDuration: 0,
                minStarDuration: 0,
                // 地面节点，用于确定星星生成的高度
                ground:{
                    default: null,
                    type: cc.Node
                },
                // player 节点，用于获取主角弹跳的高度，和控制主角行动开关
                player:{
                    default: null,
                    type: cc.Node
                },
                scoreDisplay: {
                    default: null,
                    type: cc.Label
                },
                // 得分音效资源
        scoreAudio: {
            default: null,
            url: cc.AudioClip
        }
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    onLoad: function () {
        // this.player.stopAllActions();
        var teplay=this.player.getComponent('Player')
        console.log('====onLoad=========')
        console.log(teplay)
        teplay.stopAllActions()
        // 初始化计时器
        this.timer = 0;
        this.starDuration = 0;
        // 初始化计分
        this.score = 0;
        // 获取地平面的 y 轴坐标
        this.groundY = this.ground.y + this.ground.height/2;
        // 生成一个新的星星
        this.spawnNewStar();
    },
    spawnNewStar: function() {
        // 使用给定的模板在场景中生成一个新节点
        var newStar = cc.instantiate(this.starPrefab);
        newStar.setLocalZOrder(5)
        // 将新增的节点添加到 Canvas 节点下面
        this.node.addChild(newStar);
        // 将 Game 组件的实例传入星星组件
        newStar.getComponent('Star').game = this;
        // 为星星设置一个随机位置
        newStar.setPosition(this.getNewStarPosition());
        // 重置计时器，根据消失时间范围随机取一个值
        this.starDuration = this.minStarDuration + cc.random0To1() * (this.maxStarDuration - this.minStarDuration);
        this.timer = 0;
    },
    getNewStarPosition: function () {
        var randX = 0;
        // 根据地平面位置和主角跳跃高度，随机得到一个星星的 y 坐标
        var randY = this.groundY + cc.random0To1() * this.player.getComponent('Player').jumpHeight + 50;
        // 根据屏幕宽度，随机得到一个星星 x 坐标
        var maxX = this.node.width/2;
        randX = cc.randomMinus1To1() * maxX;
        // console.log('=====randX======')
        if(randX>480){randX=randX-50}
        if(randX<-480){randX=randX+50}
        // console.log(randX)
        // 返回星星坐标
        return cc.p(randX, randY);
    },
    start () {

    },
    gainScore: function () {
        this.score += 1;
        // 更新 scoreDisplay Label 的文字
        this.scoreDisplay.string = 'Score: ' + this.score.toString();
        // 播放得分音效
        cc.audioEngine.playEffect(this.scoreAudio, false);
    },
    update (dt) {
        // 每帧更新计时器，超过限度还没有生成新的星星
        // 就会调用游戏失败逻辑
        if (this.timer > this.starDuration) {
            this.gameOver();
            return;
        }
        this.timer += dt;
    },
    gameOver: function () {
        this.player.stopAllActions(); //停止 player 节点的跳跃动作
        cc.director.loadScene('game');
    }
});
